package com.zhxy.manager.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class UserLoginFilter implements Filter {
    private String[] all = new String[]{"/api/user/login.do","login.html"};
    @Override
    public void destroy() {
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        if (((HttpServletRequest) req).getSession().getAttribute("user") == null){
            String url = request.getRequestURI();
            if (url.contains(".js") || url.contains(".css") ||url.contains(".jpg")){
                chain.doFilter(request,resp);
            }else{
                for (String s :all ) {
                    if (url.contains(s)){
                        chain.doFilter(req,resp);
                    }
                }
                HttpServletResponse response = (HttpServletResponse)resp;
                response.sendRedirect("login.html");
            }
        }else{
            chain.doFilter(req,resp);
        }
    }
    @Override
    public void init(FilterConfig config) throws ServletException {
    }
}
